
var com = {};

//dom加载完成后执行的js
;$(function () {

    //全选的实现
    $(".check-all").click(function () {
        $(".ids").prop("checked", this.checked);
    });
    $(".ids").click(function () {
        var option = $(".ids");
        option.each(function (i) {
            if (!this.checked) {
                $(".check-all").prop("checked", false);
                return false;
            } else {
                $(".check-all").prop("checked", true);
            }
        });
    });

    //ajax get请求
    $('.ajax-get').click(function () {
        var target;
        var that = this;
        if ($(this).hasClass('confirm')) {
            // if(!confirm('确认要执行该操作吗?')){
            //     return false;
            // }
            com.msg_confirm('确认要执行该操作吗?',function () {
                if ((target = $(that).attr('href'))) {
                    $.get(target).success(function (data) {
                        if (data.status == 1) {
                            layer.msg(data.info + ' 页面即将自动跳转~', {
                                icon: 6,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                if (data.url) {
                                    location.href = data.url;
                                }
                            });
                        } else {
                            layer.msg(data.info, {
                                icon: 5,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                if (data.url) {
                                    location.href = data.url;
                                }
                            });
                        }
                    });

                }
            });
        }
        return false;
    });

    //ajax post submit请求
    $('.ajax-post').click(function () {
        var target, query, form;
        var target_form = $(this).attr('target-form');
        var that = this;
        var nead_confirm = false;
        if (($(this).attr('type') == 'submit') || (target = $(this).attr('href')) || (target = $(this).attr('url'))) {
            form = $('.' + target_form);

            if ($(this).attr('hide-data') === 'true') {//无数据时也可以使用的功能
                form = $('.hide-data');
                query = form.serialize();
            } else if (form.get(0) == undefined) {
                return false;
            } else if (form.get(0).nodeName == 'FORM') {
                if ($(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                if ($(this).attr('url') !== undefined) {
                    target = $(this).attr('url');
                } else {
                    target = form.get(0).action;
                }
                query = form.serialize();
            } else if (form.get(0).nodeName == 'INPUT' || form.get(0).nodeName == 'SELECT' || form.get(0).nodeName == 'TEXTAREA') {
                form.each(function (k, v) {
                    if (v.type == 'checkbox' && v.checked == true) {
                        nead_confirm = true;
                    }
                })
                if (nead_confirm && $(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                query = form.serialize();
            } else {
                if ($(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                query = form.find('input,select,textarea').serialize();
            }
            var load_index = layer.load();
   
            $.post(target, query).success(function (data) {
                layer.close(load_index);
                if (data.status == 1) {
                    layer.msg(data.info + ' 页面即将自动跳转~', {
                        icon: 6,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        if (data.url) {
                            location.href = data.url;
                        }
                    });
                } else {
                    layer.msg(data.info, {
                        icon: 5,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        if (data.url) {
                            location.href = data.url;
                        }
                    });
                }
            });
        }
        return false;
    });

});

com.msg_success = function (info) {
    layer.msg(info, {
        icon: 6,
        time: 1000 * 2
    });
};

com.msg_error = function (info) {
    layer.msg(info, {
        icon: 5,
        time: 1000 * 2
    });
};

com.msg_confirm = function (info, func) {
    layer.confirm(info, {icon: 3, title: '提示'}, function (index) {
        layer.close(index);
        func();
    });
};

com.msg_load = function () {
    var index = layer.load(1);
    return index;
};

com.msg_load_close = function () {
    layer.closeAll('loading'); //关闭加载层
};


com.ajax = function (options) {
    options = $.extend({
        showLoading:true
    }, options);

    var ajaxDefaults = {
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        error: function (e) {
            var msg = e.responseText;
            com.msg_error( msg);
        }
    };

    if (options.showLoading) {
        ajaxDefaults.beforeSend = com.msg_load;
        ajaxDefaults.complete = com.msg_load_close;
    }

    $.ajax($.extend(ajaxDefaults, options));
};
